package simple;

import java.util.HashMap;
import java.util.Map;

class Solution1 {
    public int[] twoSum(int[] nums, int target) {
        int size = nums.length;
        //数值存key，索引存value
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();

        //先查再存，避免重复元素使用
        for(int i=0;i<size;i++){
            if(map.containsKey(target - nums[i])){
                return new int[]{i,map.get(target - nums[i])};
            }
            map.put(nums[i],i);
        }
        return new int[0];
    }
}

